package com.amigo.online.provider.user.controller;


import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.group.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.util.CommonValidator;
import com.amigo.online.provider.user.exception.UserException;
import com.amigo.online.provider.user.service.GroupService;

/**
 * 群聊
 *
 * @author kml
 */
@RestController
@RequestMapping(value = "/group", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class GroupController extends BaseController {

    @Autowired
    GroupService groupService;
    @PostMapping(value = "/newGroup")
    public ResponseEntity<Response> newGroup(@RequestBody NewGroupDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(groupService.newGroup(dto));
    }

    @PutMapping(value = "/quitGroup")
    @IgnoreSecurity
    public ResponseEntity<Response> quitGroup(@RequestBody QuitGroupDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(groupService.quitGroup(dto));
    }

    @PutMapping(value = "/editGroup")
    public ResponseEntity<Response> editGroup(@RequestBody EditGroupDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(groupService.editGroup(dto));
    }

    /**
     * 修改群头像
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/updateGroup")
    public ResponseEntity<Response> updateGroup(@RequestBody UpdateGroupDto dto) {
        return success(groupService.updateGroup(dto));
    }

    @GetMapping(value = "/getPullableFriends/{groupId}")
    public ResponseEntity<Response> getPullableFriends(@PathVariable Long groupId) {
        return success(groupService.getPullableFriends(groupId));
    }

    @PostMapping(value = "/addGroupMember")
    public ResponseEntity<Response> addGroupMember(@RequestBody AddGroupMemberDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(groupService.addGroupMember(dto));
    }

    @GetMapping(value = "/getMyGroup")
    public ResponseEntity<Response> getMyGroup() {
        return success(groupService.getMyGroup());
    }

    /**
     * 根据群ID查群成员信息
     *
     * @return
     */
    @GetMapping(value = "/getMembers/{groupId}/{page}/{size}")
    public ResponseEntity<Response> getMembersByGroupId(@PathVariable Long groupId, @PathVariable Integer page, @PathVariable Integer size) {
        if (groupId == null || groupId <= 0 || page == null || size == null) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(groupService.getMembersByGroupId(groupId, page, size));
    }

    /**
     * 根据群ID查群成员数量
     *
     * @return
     */
    @GetMapping(value = "/getMembersCount/{groupId}")
    public ResponseEntity<Response> getMembersCountByGroupId(@PathVariable Long groupId) {
        if (groupId == null || groupId <= 0) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(groupService.getMembersCountByGroupId(groupId));
    }

    /**
     * 附近的群(page从1开始)
     *
     * @param
     * @return
     */
    @GetMapping(value = "/nearby/{page}")
    public ResponseEntity<Response> nearbyGroup(@PathVariable Integer page,
                                                @RequestHeader("lng") String lnt,
                                                @RequestHeader("lat") String lat) {
        return success(groupService.nearbyGroup(page, lnt, lat));
    }
    public static void main(String[] args) {
        System.out.println(new Distance(25000, Metrics.KILOMETERS));
    }
    /**
     * 同步群数据到mongodb
     *
     * @param
     * @return
     */
    @IgnoreSecurity
    @GetMapping(value = "/private/sysnGroup/{startId}/{endId}")
    ResponseEntity<Response> sysnGroup(@PathVariable Integer startId, @PathVariable Integer endId) {
        return success(groupService.sysnGroup(startId, endId));
    }

    /**
     * @param
     * @return ResponseEntity<Response>
     * @throws
     * @Title: applyGroup
     * @Description: 申请进群
     */
    @GetMapping(value = "/apply")
    ResponseEntity<Response> applyGroup(@RequestBody ApplyGroupDto dto) {
        return success(groupService.applyGroup(dto));
    }

    /**
     * @param @return
     * @return ResponseEntity<Response>
     * @throws
     * @Title: examineApply
     * @Description: 管理员同意或不同意进群
     */
    @PostMapping(value = "/apply/examine")
    ResponseEntity<Response> examineApply(@RequestBody AllowApplyDto allowApplyDto) {
        System.out.println(allowApplyDto.getGroupId());
        return success(groupService.allowApply(allowApplyDto));
    }

    /**
     * @Author:LiangWu
     * @Date: 2019/4/28 18:03
     * @Description:管理员获取群消息 TO_DO
     */
    @PostMapping(value = "/apply/applyRecord/")
    ResponseEntity<Response> applyRecord(@PathVariable Long groupId) {
        return success(groupService.applyRecord(groupId));
    }

    /**
     * 禁言
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/ban")
    ResponseEntity<Response> ban(@Validated BanDto dto) {
        return success(groupService.ban(dto));
    }
}
